﻿@using PagedList.Mvc;
@using PagedList;
@using TestOnlineProject.Models.Entities;
<script>
    $(document).ready(function() {
        // Select all.
        $("#select_all").click(function() {
            $(".is_selected").attr('checked', true);
            $(':disabled').attr('checked', false);
        });

        // Unselect all.
        $("#unselect_all").click(function() {
            $(".is_selected").attr('checked', false);
        });

        $("#deactivate").click(function() {
            var val = "";
            $(':checkbox:checked').each(function(i) {
                val += $(this).val() + "-";
            });

            if (val == "") {
                alert("You must select at least one user!");
            } else {
                var con = confirm("Are you sure you want to deactivate this user(s)?");
                if (con == true) {
                    // Deactivate.
                    $.ajax({
                        url: "@Url.Content("~/User/Deactivate")?ids=" + val,
                        type: 'GET',
                        success: function(result) {
                            if (result != null) {
                                alert(result);
                                // Reload page.
                                $.ajax({
                                    url: "@Url.Content("~/User/Index")?page=@ViewBag.Page&keyWord=@ViewBag.KeyWord&column=@ViewBag.Column&sort=@ViewBag.Sort",
                                    type: 'GET',
                                    success: function(data) {
                                        $("#unobtrusive").html(data);
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });

        $("#activate").click(function() {
            var val = "";
            $(':checkbox:checked').each(function() {
                val += $(this).val() + "-";
            });
            if (val == "") {
                alert("You must select at least one user!");
            } else {
                var con = confirm("Are you sure you want to activate this user(s)?");
                if (con == true) {
                    // Activate.
                    $.ajax({
                        url: "@Url.Content("~/User/Activate")?ids=" + val,
                        type: 'GET',
                        success: function(result) {
                            if (result != null) {
                                alert(result);
                                // Reload page.
                                $.ajax({
                                    url: "@Url.Content("~/User/Index")?page=@ViewBag.Page&keyWord=@ViewBag.KeyWord&column=@ViewBag.Column&sort=@ViewBag.Sort",
                                    type: 'GET',
                                    success: function(data) {
                                        $("#unobtrusive").html(data);
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });

        $("#delete").click(function() {
            var val = "";
            $(':checkbox:checked').each(function() {
                val += $(this).val() + "-";
            });
            if (val == "") {
                alert("You must select at least one user!");
            } else {
                var con = confirm("Are you sure you want to delete this user(s)?");
                if (con == true) {
                    // Delete.
                    $.ajax({
                        url: "@Url.Content("~/User/Delete")?ids=" + val,
                        type: 'GET',
                        success: function(result) {
                            if (result != null) {
                                alert(result);
                                // Reload page.
                                $.ajax({
                                    url: "@Url.Content("~/User/Index")?page=@ViewBag.Page&keyWord=@ViewBag.KeyWord&column=@ViewBag.Column&sort=@ViewBag.Sort",
                                    type: 'GET',
                                    success: function(data) {
                                        $("#unobtrusive").html(data);
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
    });
</script>
<div id="list">
    <div>
        <table class="table table-hover" style="table-layout: fixed">
            <tr>
                <th style="vertical-align: middle; width: 50px">
                    @Ajax.ActionLink("No.", "Index", new { column = "No", sort = (@ViewBag.Sort == "Asc" && @ViewBag.Column == "No") ? "Desc" : "Asc", keyWord = @ViewBag.KeyWord }, new AjaxOptions { UpdateTargetId = "list", InsertionMode = InsertionMode.Replace })
                </th>
                <th style="vertical-align: middle">
                    @Ajax.ActionLink("Full Name", "Index", new { column = "FullName", sort = (@ViewBag.Sort == "Asc" && @ViewBag.Column == "FullName") ? "Desc" : "Asc", keyWord = @ViewBag.KeyWord }, new AjaxOptions { UpdateTargetId = "list", InsertionMode = InsertionMode.Replace })
                </th>
                <th style="vertical-align: middle">
                    @Ajax.ActionLink("Username", "Index", new { column = "Username", sort = (@ViewBag.Sort == "Asc" && @ViewBag.Column == "Username") ? "Desc" : "Asc", keyWord = @ViewBag.KeyWord }, new AjaxOptions { UpdateTargetId = "list", InsertionMode = InsertionMode.Replace })
                </th>
                <th style="vertical-align: middle; width: 50px">
                    @Ajax.ActionLink("Role", "Index", new { column = "Role", sort = (@ViewBag.Sort == "Asc" && @ViewBag.Column == "Role") ? "Desc" : "Asc", keyWord = @ViewBag.KeyWord }, new AjaxOptions { UpdateTargetId = "list", InsertionMode = InsertionMode.Replace })
                </th>
                <th style="width: 30px">
                    <div class="btn-group">
                        <a class="btn dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu pull-right">
                            <li>
                                <button class="btn btn-link" id="select_all">
                                    Select All</button></li>
                            <li>
                                <button class="btn btn-link" id="unselect_all">
                                    Unselect All</button></li>
                            <li>
                                <button class="btn btn-link" id="deactivate">
                                    Deactivate</button></li>
                            <li>
                                <button class="btn btn-link" id="activate">
                                    Activate</button></li>
                            <li>
                                <button class="btn btn-link" id="delete">
                                    Delete</button></li>
                        </ul>
                    </div>
                </th>
            </tr>
            @{
                var row = 0;
                foreach (var user in ViewBag.onePageOfUsers as IEnumerable<User>)
                {

                    row++;
                    <tr id="row_@row">
                        <td>
                            @{
                                var noList = ViewBag.NoList as List<NoModel>;
                                var no = noList.FirstOrDefault(n => n.Id == user.UserId).No;
                                <span style="word-wrap: break-word">@no</span>
                            }
                        </td>
                        <td>
                            <span style="word-wrap: break-word">@Html.ActionLink(user.FullName, "Details", "User", new {id = @user.UserId}, null)</span>
                        </td>
                        <td>
                            <span style="word-wrap: break-word">@Html.DisplayFor(model => user.Username)</span>
                        </td>
                        <td>
                            @Html.DisplayFor(item => user.Role.Name)
                        </td>
                        <td>
                            @{
                                if (user.RoleId != 1)
                                {
                                    <input type="checkbox" value="@user.UserId" class="is_selected"/>
                                }
                                else
                                {
                                    <input type="checkbox" value="@user.UserId" class="is_selected" disabled/>
                                }
                            }
                        </td>
                        @if (!user.IsActivated)
                        {
                            <script>
                                $("#row_@row").addClass("deactivated_item");
                            </script>
                        }
                    </tr>
                }
            }
        </table>
    </div>
    @Html.PagedListPager((IPagedList)ViewBag.onePageOfUsers, page => Url.Action("Index", new { page, sort = @ViewBag.Sort, column = @ViewBag.Column, keyWord = @ViewBag.KeyWord }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "unobtrusive", OnComplete = "" }))
</div>